package com.zxl.yjb.activity.home.sharemessage;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.zxl.yjb.R;
import com.zxl.yjb.adapter.ShareMsgAdapter;
import com.zxl.yjb.base.BaseActivity;
import com.zxl.yjb.bean.ShareMsgBean;
import com.zxl.yjb.http.manager.DataManager;
import com.zxl.yjb.http.observer.ObserverOnNextListener;
import com.zxl.yjb.http.observer.ProgressObserver;
import com.zxl.yjb.utils.DisplayUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShareMessageCollectionActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView iv_back;
    @BindView(R.id.tv_page_title)
    TextView tv_page_title;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    @BindView(R.id.sm_refresh)
    SmartRefreshLayout sm_refresh;


    private int page = 1;
    private int count = 10;

    private DataManager dataManager;

    private ShareMsgAdapter shareMsgAdapter;
    private ArrayList<ShareMsgBean> shareMsgBeans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sharemessage_collection);
        ButterKnife.bind(this);
        initView();
        getData();
    }

    @Override
    protected void initView() {
        tv_page_title.setText("收藏信息");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        rv_list.setLayoutManager(linearLayoutManager);
        shareMsgAdapter = new ShareMsgAdapter(R.layout.iten_share_msg, shareMsgBeans);
        rv_list.setAdapter(shareMsgAdapter);
        shareMsgAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(ShareMessageCollectionActivity.this, ShareMessageDetailActivity.class);
                intent.putExtra("id", shareMsgBeans.get(position).getTopicid());
                startActivity(intent);
            }
        });

        shareMsgAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, final int position) {

                DisplayUtil.diaplayDialog(ShareMessageCollectionActivity.this, "提示", "确定要删除此信息吗？", "取消", "删除", null,new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        del(shareMsgBeans.get(position).getId(),position);
                    }
                });
                return true;
            }
        });
        sm_refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();

            }
        }).setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
            }
        });
    }


    private void getData() {
        dataManager = new DataManager();
        Map<String,String> map = new HashMap<>();
        map.put("page",page+"");
        map.put("count",count+"");
        ObserverOnNextListener<ArrayList<ShareMsgBean>> listener = new ObserverOnNextListener<ArrayList<ShareMsgBean>>() {
            @Override
            public void onNext(ArrayList<ShareMsgBean> shareMsgBeansList) {
                if (page == 1)
                    shareMsgBeans.clear();
                shareMsgBeans.addAll(shareMsgBeansList);
                shareMsgAdapter.notifyDataSetChanged();

                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        };
        dataManager.getCollection(new ProgressObserver<ArrayList<ShareMsgBean>>(listener, this) {
            @Override
            public void onError(Throwable e) {
                super.onError(e);
                sm_refresh.finishRefresh();
                sm_refresh.finishLoadMore();
            }
        }, map);
    }

    private void del(String id,final int position)
    {
        dataManager = new DataManager();
        Map<String, String> map = new HashMap<>();
        map.put("id", id);
        ObserverOnNextListener<Object> listener = new ObserverOnNextListener<Object>() {
            @Override
            public void onNext(Object o) {
                shareMsgBeans.remove(position);
                shareMsgAdapter.notifyDataSetChanged();
            }
        };
        dataManager.delCollection(new ProgressObserver<Object>(listener, ShareMessageCollectionActivity.this), map);
    }


    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
